package com.oocl.interaction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.net.SocketException;

import com.oocl.global.GlobalVar;
import com.oocl.server.po.ClientInfo;
import com.oocl.server.po.Deptment;
import com.oocl.server.po.Person;
import com.oocl.client.ClientInfoList;
import com.oocl.command.CommandHandle;
import com.oocl.dao.DeptmentDao;
import com.oocl.dao.PersonDao;
import com.oocl.dao.impl.DeptmentDaoImpl;
import com.oocl.dao.impl.PersonDaoImpl;

public class Chater implements Runnable {

	private static Socket socket;

	public Chater(Socket socket) {
		super();
		Chater.socket = socket;
	}

	public void chat() throws IOException, ParseException {
		ClientInfoList cInfoList = new ClientInfoList(socket);
		cInfoList.addClientInfo();
		
		OutputStream out = socket.getOutputStream();
		InputStream in = socket.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));

		out.write("TCP Server Start Now\n".getBytes());
		//out.write("Please input your command to operate the Person-Department database.\n".getBytes());
		while (true) {
			if (GlobalVar.isOpenTcpServer) {
				try {
					out.write("Please input your command to operate the Person-Department database.\n".getBytes());
					out.write("over\n".getBytes());
					String line = br.readLine();
					CommandHandle ch = new CommandHandle(br, out, socket);
					ch.handleCommandFromTcpClient(line);
					
				} catch (SocketException e) {
					out.close();
					br.close();
					in.close();
					break;
				}

			}
		}
	}
	
	@Override
	public void run() {
		try {
			System.out.println("chater!!!!!!!!!!!!!!");
			chat();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
